import processing.core.PApplet;

public class graph {
	boolean hover = false;
	boolean pressing = false;
	boolean showValues = false;
	boolean selected = false;
	boolean resizeSel = false;
	boolean rename = false;

	// position in graphs array
	int i;

	// default dimensions
	// box dimensions:
	int x = 200;
	int y = 200;
	int w = 200;
	int h = 200;

	// select box dimensions:
	int _x = 0;
	int _y = y - 0;
	int _w = 20;
	int _h = 20;

	PApplet parent;

	public graph(PApplet p) {
		parent = p;
		new RRect(parent);
	}
	
	public graph() {
		
	}

	int[] graphColor = { 210, 210, 210, 255 };
	int _color = 255;
	int _colorSel = 230;
	String label = "default";

	void init(int x, int y, int w, int h, String label, boolean showValues,
			int i) {
		this.x = x;
		this.y = y;
		this.w = w;
		this.h = h;
		this.label = label;
		this.showValues = showValues;
		this._x = 0;
		this._y = 0;
		this.i = i;
		display();
	}

	void init(int x, int y, int w, int h, String label, boolean showValues,
			int i, int startPosX, int startPosY) {
		this.x = x;
		this.y = y;
		this.w = w;
		this.h = h;
		this.label = label;
		this.showValues = showValues;
		this._x = startPosX;
		this._y = startPosY;
		this.i=i;
		display();
	}

	void display() {
		if (_x > w) {
			_x = w;
		}
		if (_y > h) {
			_y = h;
		}
		if (Main.editMode && selected) {
			parent.fill(110, 110, 110, 255);
			parent.rect(x, y, w, h);
			parent.fill(230);
			parent.rect(_x + x - _w / 2, _y + y - _h / 2, _w, _h);
			parent.fill(0);
			parent.text(label, (float)(x + w / 6. - 20), (float)(y + h + 30));
		} else {
			if (hover) {
				parent.fill(230, 230, 230, 255);
				parent.rect(x, y, w, h);
				parent.fill(_color);
				parent.rect(_x + x - _w / 2, _y + y - _h / 2, _w, _h);
				parent.fill(0);
				parent.text(label, (float)(x + w / 6. - 20), (float)(y + h + 30));
			} else {
				parent.fill(graphColor[0], graphColor[1], graphColor[2], graphColor[3]);
				parent.rect(x, y, w, h);
				parent.fill(_color);
				parent.rect(_x + x - _w / 2, _y + y - _h / 2, _w, _h);
				parent.fill(0);
				parent.text(label, (float)(x + w / 6. - 20), (float)(y + h + 30));
			}

			if (pressing) {
				UIActions.graphAction(i);
				_x = parent.mouseX - x;
				_y = parent.mouseY - y;
				if (_x < 0) {
					_x = 0;
				}
				if (_x > w) {
					_x = w;
				}
				if (_y < 0) {
					_y = 0;
				}
				if (_y > h) {
					_y = h;
				}
				parent.fill(230, 230, 230, 255);
				parent.rect(x, y, w, h);
				parent.fill(_color);
				parent.rect(_x + x - _w / 2, _y + y - _h / 2, _w, _h);
				parent.fill(0);
				parent.text(label, (float)(x + w / 6. - 20), (float)(y + h + 30));
			}
		}
		if (showValues) {
			parent.fill(0);
			String positionY = String.format("%.2f",((h - _y) / (float) (h)));
			String positionX = String.format("%.2f",(_x / (float) (w)));
			String positionXY = "0";
			if (w > 0 && h > 0) {
				positionXY = ("(" + positionX + "," + positionY + ")");
			} else if (h > 0) {
				positionXY = ("" + positionY);
			} else {
				positionXY = ("" + positionX);
			}
			parent.text(positionXY, _x + x + _w / 2 + 10, _y + y - 5);
		}
	}
	Object[] toObj() {
		Object[] object = new Object[17];
		object[0]=i;
		object[1]=x;
		object[2]=y;
		object[3]=w;
		object[4]=h;
		object[5]=_x;
		object[6]=_y;
		object[7]=_w;
		object[8]=_h;
		object[9]=graphColor[0];
		object[10]=graphColor[1];
		object[11]=graphColor[2];
		object[12]=graphColor[3];
		object[13]=_color;
		object[14]=_colorSel;
		object[15]=label;
		object[16]=showValues;
		return object;
	}
}
